import 'dart:convert' show json;

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }

  return null;
}

class Root {
  Root({
    this.code,
    this.data,
  });

  factory Root.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<MaterialOrder> data = jsonRes['data'] is List ? <MaterialOrder>[] : null;
    if (data != null) {
      for (final dynamic item in jsonRes['data']) {
        if (item != null) {
          data.add(MaterialOrder.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return Root(
      code: asT<int>(jsonRes['code']),
      data: data,
    );
  }

  int code;
  List<MaterialOrder> data;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'code': code,
        'data': data,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class MaterialOrder {
  MaterialOrder({
    this.brand,
    this.products,
  });

  factory MaterialOrder.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<Product> products = jsonRes['products'] is List ? <Product>[] : null;
    if (products != null) {
      for (final dynamic item in jsonRes['products']) {
        if (item != null) {
          products.add(Product.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    return MaterialOrder(
      brand: Brand.fromJson(asT<Map<String, dynamic>>(jsonRes['brand'])),
      products: products,
    );
  }

  Brand brand;
  List<Product> products;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'brand': brand,
        'products': products,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class Brand {
  Brand({
    this.id,
    this.name,
    this.logo,
  });

  factory Brand.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : Brand(
          id: asT<int>(jsonRes['id']),
          name: asT<String>(jsonRes['name']),
          logo: asT<String>(jsonRes['logo']),
        );

  int id;
  String name;
  String logo;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'name': name,
        'logo': logo,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class Product {
  Product({
    this.id,
    this.cover,
    this.code,
    this.size,
    this.hsName,
    this.hsCode,
    this.add_time,
    this.update_time,
  });

  factory Product.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : Product(
          id: asT<int>(jsonRes['id'] ?? jsonRes['hs_id']),
          cover: asT<String>(jsonRes['cover'] ?? jsonRes['hs_cover']),
          code: asT<String>(jsonRes['code'] ?? jsonRes['hs_code']),
          size: asT<String>(jsonRes['size'] ?? jsonRes['hs_size']),
          hsName: asT<String>(jsonRes['hs_name'] ?? jsonRes['hs_name']),
          add_time: asT<String>(jsonRes['add_time']),
          update_time: asT<String>(jsonRes['update_time']),
          hsCode: asT<String>(jsonRes['hs_code']),
        );

  bool select = false;
  bool delete = false;
  int id;
  String cover;
  String code;
  String size;
  String hsCode;
  String hsName;
  String add_time;
  String update_time;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'cover': cover,
        'code': code,
        'size': size,
        'hs_name': hsName,
        'add_time': add_time,
        'update_time': update_time,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
